# Copyright (c) 2023 Presto Labs Pte. Ltd.
# Author: tianqiang

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo

def generate():
  mea = 'Spot.Bittrex.v3'
  me = 'Spot.Bittrex'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://api.bittrex.com/v3/markets').json()

  pis = []
  for info in data:
    if info['status'] != 'ONLINE':
      continue

    native_symbol: str = info['symbol']
    native_base = info['baseCurrencySymbol']
    native_quote = info['quoteCurrencySymbol']
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)

    symbol = "%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=1.0 / pow(10, int(info['precision'])),
        qty_ticksize=0.00000001,
        min_qty=float(info['minTradeSize']),
        min_amount=0,
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  base_currency_list = [info['native_base'] for info in data]
  quote_currency_list = [info['native_quote'] for info in data]
  balance_currency_list = [
      info['native_base'] for info in data if info['quote'] == 'USD'] + ['USDT']
  inverse_symbol_list = [
      info['native_symbol'] for info in data if info['quote'] == 'USD']
  usdt_symbol_list = [
      info['native_symbol'] for info in data if info['quote'] == 'USDT']
  res = {
      'base_currency_list': list(set(base_currency_list)),
      'quote_currency_list': list(set(quote_currency_list)),
      'balance_currency_list': list(set(balance_currency_list)),
      'inverse_symbol_list': list(set(inverse_symbol_list)),
      'usdt_symbol_list': list(set(usdt_symbol_list)),
  }
  return res


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
